package com.hxf.web.action.preferential;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.MealPreferentialDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class PreferentialAction extends WebActionBase {

	private static final long serialVersionUID = 1L;
	private List<MealMenuDto> leftList;
	private List<MealMenuDto> rightList;
	private List<MealMenuDto> mealMenuList;
	private List<MealPreferentialDto> preferentialDtoList;
	private MealPreferentialDto preferentialDto;
	private String preferentialIds;
	private List<MealPreferentialDto> preferentialDtoListAdmin;
	
	private String mealMenuIds;
	private Integer id;
	private String name;
	private String mealPackage;
	private double costPrice;
	private double preferentialPrice;
	private double discount;
	private String beginDate;
	private String endDate;
	private Integer copies;
	private String explanation;  
	private String mealMenuId;
	private Integer preferentialType;
	private String packageRemark;
	private Integer status;
	private Boolean releaseStatus;

	/**
	 * 优惠列表
	 * 
	 * @return
	 */
	public String preferentialList() {
		MealPreferentialDto mpd = new MealPreferentialDto();
		if(PublicUtil.isNotNullAndEmpty(name)){
			mpd.setName(name);
		}

		Integer restaurantId = (Integer)getSession().getAttribute("restaurantId");
		//List list = getRestaurantDto().getId()
		if(PublicUtil.isNotNull(restaurantId)){
			mpd.setRestaurantId(restaurantId);
		}
		
		try {
			ReturnData<MealPreferentialDto> result = super.serviceFacade.getPreferentialService().findMealPreferential(mpd, targetPage, pageSize);
			preferentialDtoList = result.getResultlist();
			setRecordCount(result.getRecordcount());
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "preferential-list";
	}

	/**
	 * 添加优惠页面
	 * 
	 * @return
	 */
	public String preferentialAdd() {
		try {
			MealMenuDto mmd = new MealMenuDto();
			//RestaurantDto restaurantDto = null;//getRestaurantDto();
			RestaurantDto restaurantDto = null;
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			List list = null;
			if(null != adminUserDto){
				list = (List)super.serviceFacade.getRestaurantService().findByRestaurantId(getAdminUser().getId());
			}else if(null != restaurentUserDto){
				list = (List)super.serviceFacade.getRestaurantService().findByRestaurantId(getRestaurentUserDto().getId());
			}
			
			if(null != list && 0 != list.size()){
				restaurantDto = (RestaurantDto)list.get(0);
				getRequest().setAttribute("restaurantId", restaurantDto.getId());
			}
			
			if (restaurantDto != null) {
				mmd.setRestaurantId(restaurantDto.getId());
				leftList = super.serviceFacade.getMealMenuService().findByMealMenu(mmd);
				mealMenuList = leftList;
			} else {
				leftList = new ArrayList<MealMenuDto>();
			}
			getSession().setAttribute("imagePath", null);
			getSession().removeAttribute("imagePath");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		}
		return "preferential-add-or-update";
	}
	/**
	 * 修改优惠页面
	 * @return
	 */
	public String preferentialUpdate() {
		try { 
			MealMenuDto mmd = new MealMenuDto();  
			preferentialDto = (MealPreferentialDto)super.serviceFacade.getPreferentialService().get(MealPreferentialDto.class, id);
			if (preferentialDto != null) {
				mmd.setRestaurantId(preferentialDto.getRestaurantId());
				mealMenuList = super.serviceFacade.getMealMenuService().findByMealMenu(mmd);
				String [] mealMenuId = null; 
				if (PublicUtil.isNotNullAndEmpty(preferentialDto.getMealMenuId())) {
					 mealMenuId = preferentialDto.getMealMenuId().split(",");
				}
				MealMenuDto mmDto = null;
				//右边
				rightList = new ArrayList<MealMenuDto>();
				//左边
				leftList = new ArrayList<MealMenuDto>();
				boolean addBool = true;
				if (mealMenuList != null) { 
					for (int i = 0; i < mealMenuList.size() ; i++) {
						addBool = true;
						 mmDto = mealMenuList.get(i);
						 if (mealMenuId != null) {
							 for (int j = 0;j < mealMenuId.length; j++) { 
								 if (mmDto.getId() == Integer.parseInt(mealMenuId[j])) {
									 rightList.add(mmDto);
									 addBool = false;
									 break;
								 }
							 }
						 }
						 if (addBool) {
							 leftList.add(mmDto);
						 }
					}  
				}
				 
				getSession().setAttribute("imagePath", preferentialDto.getImages());
			}   
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		 
		return "preferential-add-or-update";
	}
	
	
	/**
	 * 保存或者修改优惠信息
	 * @return
	 */
	public String preferentialSaveOrUpdate() {
		Object img = getSession().getAttribute("imagePath");
		String imgPath = "";
		if (img != null) {
			imgPath = img.toString();
		}
		
		if (PublicUtil.isNotNullAndEmpty(mealMenuId) && mealMenuId.indexOf(",") >= 0) {
			preferentialType = MealPreferentialDto.PREFERENTIAL_TYPE_1;	//套餐优惠
		} else if (PublicUtil.isNotNullAndEmpty(mealMenuId)) {
			preferentialType = MealPreferentialDto.PREFERENTIAL_TYPE_2;	//菜肴优惠
		} else {
			preferentialType = MealPreferentialDto.PREFERENTIAL_TYPE_0;	//本店优惠
		} 
		
		MealPreferentialDto mpd = new MealPreferentialDto();
		mpd.setName(name);
		mpd.setImages(imgPath);
		mpd.setMealPackage(mealPackage);
		mpd.setCostPrice(costPrice);
		mpd.setPreferentialPrice(preferentialPrice);
		mpd.setDiscount(discount);
		mpd.setBeginDate(beginDate);//开始优惠时间
		mpd.setEndDate(endDate);//结束优惠时间
		mpd.setCopies(copies);
		mpd.setExplanation(explanation);
		mpd.setRestaurantId(Integer.valueOf(null==getSession().getAttribute("restaurantId")?"0":getSession().getAttribute("restaurantId").toString()));
		mpd.setMealMenuId(mealMenuId);
		mpd.setPreferentialType(preferentialType);
		mpd.setPackageRemark(packageRemark);//备注
		mpd.setStatus(status);// 0:审核中 1:审核通过  2:审核不通过   3:作废  
		
		try {
			if (id != null && id != 0) {  
				MealPreferentialDto mpDto = (MealPreferentialDto) serviceFacade.getPreferentialService().get(MealPreferentialDto.class, id);
				mpDto.setImages(imgPath);
				mpDto.setName(name);
				mpDto.setMealMenuId(mealMenuId);
				mpDto.setPackageRemark(packageRemark);
				mpDto.setCostPrice(costPrice);
				mpDto.setPreferentialPrice(preferentialPrice); 
				mpDto.setCopies(copies);
				mpDto.setDiscount(discount);
				mpDto.setBeginDate(beginDate);
				mpDto.setEndDate(endDate);
				mpDto.setExplanation(explanation);
				mpDto.setPreferentialType(preferentialType); 
				serviceFacade.getPreferentialService().update(mpDto);//修改
			} else {
				mpd.setStatus(0);
				mpd.setReleaseStatus(false); 
				serviceFacade.getPreferentialService().save(mpd);//插入 
			}
			getSession().setAttribute("imagePath", null);
			getSession().removeAttribute("imagePath");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "preferential!preferentialList.html";
		return SUCCESS;
	}
	/**
	 * 发布和取消发布
	 * @return
	 */
	public String release() {
		try {
			serviceFacade.getPreferentialService().updateReleaseStatus(preferentialIds, releaseStatus);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 查询餐厅菜单
	 * @return
	 */
	public String findMealMenu() {
		
		return null;
	}
	
	/**
	 * 后台管理 - 餐厅优惠管理
	 * 管理员 管理 所有餐厅优惠信息 
	 */
	public String adminMealMenu(){
		try{
			
			ReturnData<MealPreferentialDto> result = super.serviceFacade.getPreferentialService().findMealPreferential(null, targetPage, pageSize);
			preferentialDtoListAdmin = result.getResultlist();
			setRecordCount(result.getRecordcount());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "admin-preferential-list";
	}
	
	/**
	 * 修改是否审核通过
	 * @return
	 */
	public String auditing(){
		try {
			serviceFacade.getPreferentialService().updateStatus(preferentialIds, status);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public List<MealMenuDto> getRightList() {
		return rightList;
	}

	public void setRightList(List<MealMenuDto> rightList) {
		this.rightList = rightList;
	}

	public String getMealMenuIds() {
		return mealMenuIds;
	}

	public void setMealMenuIds(String mealMenuIds) {
		this.mealMenuIds = mealMenuIds;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMealPackage() {
		return mealPackage;
	}

	public void setMealPackage(String mealPackage) {
		this.mealPackage = mealPackage;
	}

	public double getCostPrice() {
		return costPrice;
	}

	public void setCostPrice(double costPrice) {
		this.costPrice = costPrice;
	}

	public double getPreferentialPrice() {
		return preferentialPrice;
	}

	public void setPreferentialPrice(double preferentialPrice) {
		this.preferentialPrice = preferentialPrice;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public Integer getCopies() {
		return copies;
	}

	public void setCopies(Integer copies) {
		this.copies = copies;
	}

	public String getExplanation() {
		return explanation;
	}

	public void setExplanation(String explanation) {
		this.explanation = explanation;
	}

	 
	public String getMealMenuId() {
		return mealMenuId;
	}

	public void setMealMenuId(String mealMenuId) {
		this.mealMenuId = mealMenuId;
	}

	public Integer getPreferentialType() {
		return preferentialType;
	}

	public void setPreferentialType(Integer preferentialType) {
		this.preferentialType = preferentialType;
	}

	public String getPackageRemark() {
		return packageRemark;
	}

	public void setPackageRemark(String packageRemark) {
		this.packageRemark = packageRemark;
	}

	public List<MealPreferentialDto> getPreferentialDtoList() {
		return preferentialDtoList;
	}

	public void setPreferentialDtoList(List<MealPreferentialDto> preferentialDtoList) {
		this.preferentialDtoList = preferentialDtoList;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public MealPreferentialDto getPreferentialDto() {
		return preferentialDto;
	}

	public void setPreferentialDto(MealPreferentialDto preferentialDto) {
		this.preferentialDto = preferentialDto;
	}

	public List<MealMenuDto> getLeftList() {
		return leftList;
	}

	public void setLeftList(List<MealMenuDto> leftList) {
		this.leftList = leftList;
	}

	public List<MealMenuDto> getMealMenuList() {
		return mealMenuList;
	}

	public void setMealMenuList(List<MealMenuDto> mealMenuList) {
		this.mealMenuList = mealMenuList;
	}

	public String getPreferentialIds() {
		return preferentialIds;
	}

	public void setPreferentialIds(String preferentialIds) {
		this.preferentialIds = preferentialIds;
	}

	public Boolean getReleaseStatus() {
		return releaseStatus;
	}

	public void setReleaseStatus(Boolean releaseStatus) {
		this.releaseStatus = releaseStatus;
	}

	public List<MealPreferentialDto> getPreferentialDtoListAdmin() {
		return preferentialDtoListAdmin;
	}

	public void setPreferentialDtoListAdmin(
			List<MealPreferentialDto> preferentialDtoListAdmin) {
		this.preferentialDtoListAdmin = preferentialDtoListAdmin;
	}
	
}
